﻿using ChronoScript_Compiler.Language;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Viper.Opcodes;

namespace ChronoScript_Compiler.CodeGen
{
    public partial class CodeGen
    {
        public void CreateMethod(LanguageDefinition.Method method)
        {
            Symbols.BeginScope();
            this.ReturnType = method.type;
            Console.WriteLine(GetMethodName(method));
            Assembler.CreateGlobalLabel(GetMethodName(method));
            method.arguments.Reverse();
            foreach (LanguageDefinition.Declaration decl in method.arguments)
            {
                DeclareArgument(decl);
                Assembler.Emit(new push_ptr(Symbols[decl.Name]));
                if (getTypeFromName(decl.Type) == VType.Dword)
                    Assembler.Emit(new dstore());
            }
            method.arguments.Reverse();
            foreach (LanguageDefinition.SyntaxNode sm in method.body.body)
                CompileLine(sm);
            Assembler.Emit(new ret());
            Symbols.EndScope();
        }
    }
}
